<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>选择一个数据库</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.command.html">MongoDB::command</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.createcollection.html">MongoDB::createCollection</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodb.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB::__construct</h1>
  <p class="verinfo">(PECL mongo &gt;=0.9.0)</p><p class="refpurpose"><span class="refname">MongoDB::__construct</span> &mdash; <span class="dc-title">选择一个数据库</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoDB::__construct</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span> <code class="parameter">$conn</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>
   )</div>

  <p class="para rdfs-comment">
   这个方法通常不直接调用。推荐的用法是通过 <span class="function"><a href="mongoclient.get.html" class="function">MongoClient::__get()</a></span>
   或 <span class="function"><a href="mongoclient.selectdb.html" class="function">MongoClient::selectDB()</a></span> 方法获得对象实例。
  </p>
  <p class="para">
   如果你确实需要直接调用，可以通过以下方式：
  </p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">(</span><span style="color: #0000BB">$m</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mydbname'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <p class="para">
   但这并不好。下面的方式更加美观：
  </p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">mydbname</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;or,&nbsp;if&nbsp;the&nbsp;name&nbsp;contains&nbsp;weird&nbsp;characters:&nbsp;如果数据库名字有保留的字符：<br /><br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectDB</span><span style="color: #007700">(</span><span style="color: #DD0000">'my,db:name'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <span class="type"><a href="class.mongoclient.html" class="type MongoClient">MongoClient</a></span>
      <em><code class="parameter">conn</code></em>
     </dt>

     <dd>

      <p class="para">
       数据库链接
      </p>
     </dd>

    
    
     <dt>

      <em><code class="parameter">name</code></em>
     </dt>

     <dd>

      <p class="para">
       数据库名
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodb.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数据库对象。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-mongodb.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果数据库名称无效。抛出一个默认类型的异常
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongodb.command.html">MongoDB::command</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodb.createcollection.html">MongoDB::createCollection</a></div>
 <div class="up"><a href="class.mongodb.html">MongoDB</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
